
Imports std
Imports System.IO

Module selectwhere

    Sub Main(ByVal args() As String)
        Dim istream As FileStream = Nothing
        Dim reader As TextReader = Nothing
        Dim ostream As FileStream = Nothing
        Dim writer As TextWriter = Nothing
        Dim input As String = Nothing
        Dim parts As vector(Of String) = Nothing
        Dim i As Int32
        Dim comparer As vectorStringComparer = Nothing
        Dim factors As vector(Of String) = Nothing
        Dim baseline As vector(Of String) = Nothing
        Dim factor As String = Nothing
        Dim index As Int32

        If args.Length < 2 Or args.Length Mod 4 <> 2 Then
            raiseError("argument count error, selectwhere [columnIndex0 type0 +/-/>/< value0 " + _
                        "columnIndex1 type1 +/-/>/< value1 ... ] input output, " + _
                        "+ means >, - means <, type can be " + vectorStringComparer.typeSupport + _
                        ", if input or output is /-, use console.", errorHandle.errorType.critical)
        Else
            factors = New vector(Of String)
            baseline = New vector(Of String)
            For i = 0 To args.Length() - 6 Step 4
                If args(i + 2) = ">" Or args(i + 2) = "+" Then
                    factor = "+"
                ElseIf args(i + 2) = "<" Or args(i + 2) = "-" Then
                    factor = "-"
                End If
                factor += args(i + 1)
                factor += args(i)
                index = Convert.ToInt32(args(i))
                While (baseline.size() <= index)
                    baseline.push_back(emptyString)
                End While
                copy(Of String)(baseline(index), args(i + 3))
                factors.push_back(factor)
            Next

            comparer = New vectorStringComparer(factors)
            If args(args.Length() - 1) = "/-" Then
                writer = Console.Out
            Else
                ostream = New FileStream(args(args.Length() - 1), FileMode.Create, FileAccess.Write, FileShare.Read)
                writer = New StreamWriter(ostream)
            End If
            assert(Not writer Is Nothing, "file no found, " + args(args.Length() - 1))

            If args(args.Length() - 2) = "/-" Then
                reader = Console.In
            Else
                istream = New FileStream(args(args.Length() - 2), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                reader = New StreamReader(istream)
            End If
            assert(Not reader Is Nothing, "file no found, " + args(args.Length() - 2))

            input = reader.ReadLine
            While Not input Is Nothing
                If Not isemptystring(input) Then
                    strsepar(input, character.tab, parts)
                    If comparer.Compare(parts, baseline) > 0 Then
                        writer.WriteLine(input)
                    End If
                End If
                input = reader.ReadLine
            End While
            closestream(reader)
            closestream(writer)
        End If
    End Sub

End Module
